package com.lk.security.controller;

import com.lk.base.result.Result;
import com.lk.security.authentication.mobile.SmsSend;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: lk-security-parent
 * @description: 短信验证码控制器
 * @author: Aspirin
 * @create: 2020-05-30 22:03
 */
@Controller
public class MobileLoginController {
  public static final String SESSION_KEY = "SESSION_KEY_MOBILE_CODE";
  @Autowired SmsSend smsSend;

  /** 前往手机验证码登录页 */
  @GetMapping("/mobile/page")
  public String toMobilePage() {
    return "login-mobile";
  }

  /** 发送手机验证码 */
  @GetMapping("/code/mobile")
  @ResponseBody
  public Result smsCode(HttpServletRequest request) {
    // 1。生成手机验证码
    String code = RandomStringUtils.randomNumeric(4);
    // 2。手机验证码保存到session中
    request.getSession().setAttribute(SESSION_KEY, code);
    // 3。发送验证码到用户手机上
    String mobile = request.getParameter("mobile");
    smsSend.sendSms(mobile, code);

    return Result.ok();
  }
}
